package com.itheima.reggie.controller;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
@Api("地址簿功能")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    @ApiOperation("新增")
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        R<AddressBook> r = addressBookService.add(addressBook);
        return r;
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    @ApiOperation("设置默认地址")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        R<AddressBook> r = addressBookService.setDefault(addressBook);
        return r;
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public R get(@PathVariable Long id) {
        R r = addressBookService.get(id);
        return r;
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public R<AddressBook> getDefault() {
        R<AddressBook> r = addressBookService.getDefault();
        return r;
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    @ApiOperation("查询指定用户的全部地址")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        R<List<AddressBook>> r = addressBookService.list(addressBook);
        return r;
    }
}
